#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define NUM 101
#define STYLE '#'

void process_v1()
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    int cnt = 0;
    while(cnt <= 100)
    {
        
        printf("[%-100s][%d%%]\r", buffer, cnt);
        fflush(stdout);
        buffer[cnt] = STYLE;
        cnt++;
        usleep(10000);
    }
    printf("\n");
}

double total = 1024.0;      //总下载量
double speed = 1.0;         //速度

void process_v2(double cur)
{
    char buffer[NUM];
    memset(buffer, 0, sizeof(buffer));
    const char* lable = "|/-\\";
    int len = strlen(lable);

    static int cnt = 0;
    cnt %= len;
    int num = (int)cur * 100 / total;
    int i = 0;
    for(;i < num;++i)
        buffer[i] = STYLE;
    double rate = cur / total;
    printf("[%-100s][%.1lf%%][%c]\r", buffer, rate * 100, lable[cnt]);
    fflush(stdout);
    cnt++;
}

void DownLoad()
{
    double cur = 0;         //当前下载量
    while(cur <= total)
    {
        process_v2(cur);
        usleep(5000);
        cur += speed;
    }
    printf("\ndownload %.2lfMB Done\n", cur);
}

int main()
{ 
    DownLoad();
    return 0;
}